package com.gonsin.gview.example.gview.user;


import com.gonsin.gview.annotations.GView;
import com.gonsin.gview.annotations.actions.GAction;
import com.gonsin.gview.annotations.board.GBoard;
import com.gonsin.gview.annotations.board.GButton;
import com.gonsin.gview.annotations.board.form.*;
import com.gonsin.gview.annotations.data.*;
import com.gonsin.gview.example.bean.UserBean;
import com.gonsin.gview.example.dao.UserDao;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;

import static com.gonsin.gview.annotations.enums.ButtonAction.CONFIRM_AND_CLOSE;

@GView(
        viewName = "UpdateUser",
        title = "修改用户",
        props = {"userId"},
        boards = {
                @GBoard(form = @GForm(
                        data = @GData(fromSql = "select * from gs_user where id = ${userId}"),
                        formItems = {
                                @GFormItem(selector = @GSelector(name = "选择权限组", keyName = "groupId", listData = @GListData(
                                        fromSql = "select id as groupId, name from gs_user_group",
                                        keyName = "name",
                                        valueName = "groupId"
                                ))),
                                @GFormItem(hidden = @GHidden(keyName = "userId", valueName = "keyOnly")),
                                @GFormItem(textInput = @GTextInput(name = "真实名字", keyName = "realName", showIf = "{{item.groupId}}")),
                                @GFormItem(checkBox = @GCheckBox(name = "复选框", keyName = "cc")),
                                @GFormItem(file = @GFile(name = "文件上传", keyName = "ff", limit = 2)),
                                @GFormItem(label = @GLabel(name = "静态文本", keyName = "ll")),
                                @GFormItem(line = @GLine(name = "分割线")),
                                @GFormItem(tag = @GTag(name = "标签", keyName = "tags")),
                                @GFormItem(delaySelector = @GDelaySelector(name = "毫秒数", keyName = "delay")),
                                @GFormItem(multiSelector = @GMultiSelector(name = "多选下拉选择框", keyName = "ms", listData = @GListData(fromSql = "select id as groupId, name from gs_user_group", keyName = "groupId", valueName="name"))),
                                @GFormItem(multiTreeSelector = @GMultiTreeSelector(name = "多选树状下拉选择框", keyName = "mts", treeData = @GTreeData(fromSql = "select key_only as roomKey, name, parent_room_key as parentRoomKey from gs_room", keyName = "roomKey", valueName="name", parentKeyName = "parentRoomKey"))),
                                @GFormItem(radio = @GRadio(name = "单选框", keyName = "rad", listData = @GListData(fromSql = "select id as groupId, name from gs_user_group", keyName = "groupId", valueName="name"))),
                                @GFormItem(richText = @GRichText(name = "富文本框", keyName = "rr")),
                                @GFormItem(selector = @GSelector(name = "下拉框", keyName = "ss", listData = @GListData(fromSql = "select id as groupId, name from gs_user_group", keyName = "groupId", valueName="name"))),
                                @GFormItem(gswitch = @GSwitch(name = "滑块", keyName = "sw")),
                                @GFormItem(treeSelector = @GTreeSelector(name = "树状下拉", keyName = "trs", treeData = @GTreeData(fromSql = "select key_only as roomKey, name, parent_room_key as parentRoomKey from gs_room", keyName = "roomKey", valueName="name", parentKeyName = "parentRoomKey"))),
                                @GFormItem(treeCheckBox = @GTreeCheckBox(name = "树状复选", keyName = "tcb", treeData = @GTreeData(fromSql = "select key_only as roomKey, name, parent_room_key as parentRoomKey from gs_room", keyName = "roomKey", valueName="name", parentKeyName = "parentRoomKey"))),
                        },
                        okButton = @GButton(action = CONFIRM_AND_CLOSE, actionName = "updateUser", name = "确定")
                ))
        }
)
@Component
public class UpdateUserView {

    @Autowired
    private UserDao userDao;


    /**
     * 查询人员
     */
    @GDataSource("getUser")
    public UserBean getUser(int userId){
        return userDao.findById(userId).orElse(null);
    }

    /**
     * 修改用户组
     */
    @Transactional
    @GAction("updateUser")
    @GDataChanged({"getUser", "searchUser"})
    public void updateUser(HttpSession session, UserBean user){
        UserBean dbGroup = userDao.findById(user.getUserId()).orElse(null);
        BeanUtils.copyProperties(user, dbGroup, "keyOnly", "groupId", "type");
        userDao.saveAndFlush(dbGroup);
    }

}
